package com.kws.merchant.util;
 
 
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
 
import com.github.xiaoymin.knife4j.core.util.StrUtil;
import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
import org.apache.poi.util.Units;
import org.apache.poi.xwpf.usermodel.XWPFDocument;
import org.apache.poi.xwpf.usermodel.XWPFParagraph;
import org.apache.poi.xwpf.usermodel.XWPFRun;
import org.apache.poi.xwpf.usermodel.XWPFTable;
import org.apache.poi.xwpf.usermodel.XWPFTableCell;
import org.apache.poi.xwpf.usermodel.XWPFTableRow;
 
/**
 * poi word工具类
 * @author 32194
 */
public class PoiWordUtil {
 
 
    /**
     * 带输入输出路径替换模板方法
     * @param templateFile 输入路径
     * @param dataMap 替换的字符串
     * @param outputFile 输出路径
     * @throws IOException
     */
    public static void fillTemplate(File templateFile, Map<String, String> dataMap, File outputFile, File imageFile) throws IOException, InvalidFormatException {
 
        try (FileInputStream fis = new FileInputStream(templateFile);
             XWPFDocument document = new XWPFDocument(fis)) {
 
            //开始替换
            replaceWord(document,dataMap);
            //替换图片
            replaceImageInWord(document,imageFile);
 
            // 将修改后的文档写入到输出文件
            try (FileOutputStream fos = new FileOutputStream(outputFile.getPath())) {
                document.write(fos);
            }
        }
    }

    public static void fillTemplate(File templateFile, Map<String, String> dataMap, File outputFile) throws IOException, InvalidFormatException {

        try (FileInputStream fis = new FileInputStream(templateFile);
             XWPFDocument document = new XWPFDocument(fis)) {

            //开始替换
            replaceWord(document,dataMap);

            // 将修改后的文档写入到输出文件
            try (FileOutputStream fos = new FileOutputStream(outputFile.getPath())) {
                document.write(fos);
            }
        }
    }

    /**
     * 替换内容
     *
     * @param textMap 需要替换的信息集合
     */
    public static void replaceWord(XWPFDocument document, Map<String, String> textMap) {
        //解析替换文本段落对象
        changeText(document, textMap);
        //解析替换表格对象
        changeTable(document, textMap);
    }
 
 
    /**
     * 替换Word图形
     * @param document 模板文档
     * @param imageFile 图画地址
     * @throws IOException
     * @throws InvalidFormatException
     */
    public static void replaceImageInWord(XWPFDocument document, File imageFile) throws IOException, InvalidFormatException {
        //遍历所有行
        for (XWPFParagraph para : document.getParagraphs()) {
            for (XWPFRun run : para.getRuns()) {
                String text = run.getText(0);
                if (text != null && text.contains("REPLACE_IMAGE")) {
                    // 清除原有文本
                    run.setText("", 0);
                    // 插入新图片
                    run.addPicture(new FileInputStream(imageFile),
                            XWPFDocument.PICTURE_TYPE_PNG, imageFile.getName(),
                            Units.toEMU(200), Units.toEMU(200));
                }
            }
        }
    }
 
 
 
    /**
     * 替换段落文本
     *
     * @param document docx解析对象
     * @param textMap  需要替换的信息集合
     */
    public static void changeText(XWPFDocument document, Map<String, String> textMap) {
        //获取段落集合
        List<XWPFParagraph> paragraphs = document.getParagraphs();
        for (XWPFParagraph paragraph : paragraphs) {
            //判断此段落时候需要进行替换
            String text = paragraph.getText();
            if (StrUtil.isNotBlank(text) && text.contains("${")) {
                List<XWPFRun> runs = paragraph.getRuns();
                for (XWPFRun run : runs) {
                    //替换模板原来位置
                    String textValue = changeValue(run.toString(), textMap);
                    run.setText(textValue, 0);
                }
            }
        }
    }
 
    /**
     * 替换表格对象方法
     *
     * @param document docx解析对象
     * @param textMap  需要替换的信息集合
     */
    public static void changeTable(XWPFDocument document, Map<String, String> textMap) {
        //获取表格对象集合
        List<XWPFTable> tables = document.getTables();
        for (int i = 0; i < tables.size(); i++) {
            //只处理行数大于等于2的表格，且不循环表头
            XWPFTable table = tables.get(i);
            if (table.getRows().size() > 1) {
                //判断表格内容是否可以替换
                String cellText = table.getText();
                if (StrUtil.isNotBlank(cellText) && cellText.contains("${")){
                    List<XWPFTableRow> rows = table.getRows();
                    //遍历表格,并替换模板
                    eachTable(rows, textMap);
                }
            }
        }
    }
 
 
    /**
     * 遍历表格
     *
     * @param rows    表格行对象
     * @param textMap 需要替换的信息集合
     */
    public static void eachTable(List<XWPFTableRow> rows, Map<String, String> textMap) {
        for (XWPFTableRow row : rows) {
            List<XWPFTableCell> cells = row.getTableCells();
            for (XWPFTableCell cell : cells) {
                //判断单元格内容是否可以替换
                String cellText = cell.getText();
                if (StrUtil.isNotBlank(cellText) && cellText.contains("${")) {
                    List<XWPFParagraph> paragraphs = cell.getParagraphs();
                    for (XWPFParagraph paragraph : paragraphs) {
                        List<XWPFRun> runs = paragraph.getRuns();
                        for (XWPFRun run : runs) {
                            run.setText(changeValue(run.toString(), textMap), 0);
                        }
                    }
                }
            }
        }
    }
 
    /**
     * 匹配传入信息集合与模板
     *
     * @param value   模板需要替换的区域
     * @param textMap 传入信息集合
     * @return 模板需要替换区域信息集合对应值
     */
    public static String changeValue(String value, Map<String, String> textMap) {
        Set<Entry<String, String>> textSets = textMap.entrySet();
        for (Entry<String, String> textSet : textSets) {
            //匹配模板与替换值 格式${key}
            String key = "${" + textSet.getKey() + "}";
//            String key = textSet.getKey();
            if (value.indexOf(key) != -1) {
                value = value.replace(key, textSet.getValue());
            }
        }
        return value;
    }
}